package com.xyh.concurrency;


import com.xyh.concurrency.example.thredLocal.RequestHolder;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 过滤器Ø
 */
@Slf4j
public class HttpFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //拦截器init 先执行
        System.err.println("============1============");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.err.println("============2============");
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        long id = Thread.currentThread().getId();
        System.out.println("这个id" + id);

        RequestHolder.add(id);
        filterChain.doFilter(servletRequest, servletResponse);
    }


    @Override
    public void destroy() {
        System.err.println("============6============");
    }
}
